//
//  WMBaseNavigationController.swift
//  deWaterMarkingProject
//
//  Created by admin on 2025/9/5.
//

import UIKit

class WMBaseNavigationController: UINavigationController, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
        self.interactivePopGestureRecognizer?.delegate = self
        setNavigationBar(UIColor(valueRGB: UInt(OEMInfo.navBarBgHex), alpha: 1),
                         UIColor(valueRGB: UInt(OEMInfo.navBarTitleHex), alpha: 1))
    }
    
    //MARK: - 状态栏
    func setStatusBarStyle(light: Bool){
        barLight = light
        self.setNeedsStatusBarAppearanceUpdate()
    }
    
    var barLight = false
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return (barLight ? .lightContent : .default)
    }
    
    // MARK: - 基础设置
    func setNavigationBar(_ bgColor : UIColor, _ textColor : UIColor) {
        
        let titleTextFont = UIFont(name: AppFont.smemibold, size: 18) ?? UIFont.boldSystemFont(ofSize: 18)
        let titleTextAttributes: [NSAttributedString.Key : Any] = [.foregroundColor: textColor, .font: titleTextFont]
        
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = bgColor //背景色
            appearance.shadowColor = UIColor.clear //去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
            appearance.titleTextAttributes = titleTextAttributes
            self.navigationBar.scrollEdgeAppearance = appearance //带scroll滑动的页面
            self.navigationBar.standardAppearance = appearance //常规页面
        }else {
            self.navigationBar.setBackgroundImage(UIImage.color(bgColor), for: .default) //背景色
            self.navigationBar.shadowImage = UIImage.color(bgColor) //去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
            self.navigationBar.barTintColor = bgColor
            self.navigationBar.titleTextAttributes = titleTextAttributes
        }
        self.navigationBar.isTranslucent = false
    }
    
    
    
    // MARK: - override
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if viewControllers.count > 0 {
            let backBtn = UIButton(type: .custom)
            backBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
            backBtn.contentHorizontalAlignment = .left
            backBtn.setImage(UIImage(named: "Public_Navi_Left_Black"), for: .normal)
            backBtn.addTarget(self, action: #selector(naviBackAct), for: .touchUpInside)
            viewController.hidesBottomBarWhenPushed = true
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
        }
        super.pushViewController(viewController, animated: true)
    }
    
    // MARK: - 返回按钮点击事件
    @objc func naviBackAct() {
        self.popViewController(animated: true)
    }
    
    // MARK: - 首页侧滑手势BUG
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        self.interactivePopGestureRecognizer?.isEnabled = self.viewControllers.count > 1
    }
    
    // MARK: - iOS14多层级返回BUG
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        if self.viewControllers.count > 1 {
            self.topViewController?.hidesBottomBarWhenPushed = false
        }
        return super.popToRootViewController(animated: animated)
    }
       
}

extension WMBaseNavigationController: UIGestureRecognizerDelegate {
    // 手势识别器是否能够开始识别手势.
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        true
    }
    
    // 解决有水平方向滚动的 UIScrollView 时边缘返回手势失效的问题
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        true
    }
    
    // 解决有水平方向滚动的 UIScrollView 时边缘返回手势失效的问题
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        gestureRecognizer == interactivePopGestureRecognizer
    }
}
